package vn.com.vega.chacha.v2.fragment;

import java.lang.ref.WeakReference;

import vn.com.vega.chacha.R;
import vn.com.vega.chacha.v2.Appinstant;
import vn.com.vega.chacha.v2.ArtistDetail;
import vn.com.vega.chacha.v2.PlayerScreen;
import vn.com.vega.chacha.v2.player.PlayerService;
import vn.com.vega.music.clientserver.JsonArtist;
import vn.com.vega.music.objects.Artist;
import vn.com.vega.music.objects.Song;
import vn.com.vega.music.utils.Util;
import vn.com.vega.music.view.custom.LoadingView;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

public class PlayerArtistInfoFragment extends Fragment implements
		OnClickListener {
	private ImageView mArtistThumb;
	private TextView mArtistName;
	private TextView mSongCount;
	private TextView mVideoCount;
	private TextView mAlbumCount;
	private TextView mArtistDes;
	private LoadingView mLoadingView;
	private View viewDetailArtist;
	private PlayerScreen mPlayerScreen;
	private Artist mArtist;
	private Appinstant mApp;
	private LoadArtistInfo mLoadInfo;
	private int mCurrentArtistId;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		mPlayerScreen = (PlayerScreen) getActivity();
		mApp = (Appinstant) mPlayerScreen.getApplication();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View root = inflater.inflate(R.layout.player_artist_info_view, null);
		mArtistThumb = (ImageView) root.findViewById(R.id.imv_artist_thumb);
		mArtistName = (TextView) root.findViewById(R.id.tv_artist_name);
		mSongCount = (TextView) root.findViewById(R.id.tv_artist_number_song);
		mVideoCount = (TextView) root.findViewById(R.id.tv_artist_number_video);
		mAlbumCount = (TextView) root.findViewById(R.id.tv_artist_number_album);
		mArtistDes = (TextView) root.findViewById(R.id.tv_artist_des);
		mLoadingView = (LoadingView) root
				.findViewById(R.id.loadingView_player_artist_info);
		mLoadingView.setBackgroundColor(Color.parseColor("#ffffff"));
		viewDetailArtist = root.findViewById(R.id.layout_artist_main_info);
		viewDetailArtist.setOnClickListener(this);

		mArtistThumb.setOnClickListener(this);
		Song currentSong = Util.getCurrentSongPlay();
		if (currentSong != null) {
			mCurrentArtistId = currentSong.artist_id;
		}
		return root;
	}

	BroadcastReceiver receiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			if (PlayerService.PLAYSTATE_CHANGED.equalsIgnoreCase(action)) {
				Song currentSong = Util.getCurrentSongPlay();
				if (mCurrentArtistId != currentSong.artist_id) {
					mCurrentArtistId = currentSong.artist_id;
					if (mLoadInfo != null) {
						mLoadInfo.cancel(true);
					}
					mLoadInfo = new LoadArtistInfo(
							PlayerArtistInfoFragment.this, mCurrentArtistId);
					mLoadInfo.execute();
				}

			}
		}
	};

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mLoadInfo != null) {
			mLoadInfo.cancel(true);
		}
		mPlayerScreen.unregisterReceiver(receiver);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (mArtist != null) {
			showArtistInfo();
		} else {
			if (mLoadInfo != null) {
				mLoadInfo.cancel(true);
			}
			mLoadInfo = new LoadArtistInfo(this, mCurrentArtistId);
			mLoadInfo.execute();
		}
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(PlayerService.PLAYSTATE_CHANGED);
		mPlayerScreen.registerReceiver(receiver, intentFilter);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.layout_artist_main_info:
		case R.id.imv_artist_thumb:
			String artistKeyName = PlayerArtistInfoFragment.class.getName();
			mApp.tranportArtis.put(artistKeyName, mArtist);
			Intent intent = new Intent(mPlayerScreen, ArtistDetail.class);
			intent.putExtra(ArtistDetail.ARTIST_KEY_NAME, artistKeyName);
			mPlayerScreen.startActivity(intent);
			break;

		default:
			break;
		}
	}

	private void showArtistInfo() {
		if (mArtist == null) {
			mLoadingView.loadingFinish(false, "Chưa có thông tin");
			return;
		}
		mLoadingView.loadingFinish(true, null);
		mApp.mImageLoader.download(mArtist.imageUrl, mArtistThumb);
		mArtistName.setText(mArtist.name);
		mSongCount.setText(mArtist.songCount + " bài hát");
		mVideoCount.setText(mArtist.videoCount + " video");
		mAlbumCount.setText(mArtist.albumCount + " album");
		if (Util.checkStringIsemptyOrNull(mArtist.description)) {
			mArtistDes.setText("Đang cập nhật thông tin.");
		} else {
			mArtistDes.setText(Html.fromHtml(mArtist.description));
		}
	}

	static class LoadArtistInfo extends AsyncTask<Void, Void, Void> {
		private WeakReference<PlayerArtistInfoFragment> mPlayerArtistRfc;
		private int mArtistId;

		public LoadArtistInfo(PlayerArtistInfoFragment artistInfoFragment,
				int artistId) {
			mPlayerArtistRfc = new WeakReference<PlayerArtistInfoFragment>(
					artistInfoFragment);
			mArtistId = artistId;
		}

		private boolean checkIsLive() {
			PlayerArtistInfoFragment artistInfoFragment = mPlayerArtistRfc
					.get();
			if (artistInfoFragment == null) {
				return false;
			}
			if (artistInfoFragment.mPlayerScreen == null
					|| artistInfoFragment.mPlayerScreen.isDestroy) {
				return false;
			}
			return true;
		}

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return null;
			}
			JsonArtist jsonArtist = JsonArtist.loadArtistInfo(mArtistId);
			if (jsonArtist.isSuccess()) {
				PlayerArtistInfoFragment artistInfoFragment = mPlayerArtistRfc
						.get();
				artistInfoFragment.mArtist = jsonArtist.artists.get(0);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (isCancelled() || !checkIsLive()) {
				return;
			}
			PlayerArtistInfoFragment artistInfoFragment = mPlayerArtistRfc
					.get();
			artistInfoFragment.showArtistInfo();
		}

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			if (!checkIsLive()) {
				return;
			}
			PlayerArtistInfoFragment artistInfoFragment = mPlayerArtistRfc
					.get();
			artistInfoFragment.mLoadingView.startLoading();
		}

	}
}
